package com.nami.web.facade.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.nami.store.api.request.user.UserAddRequest;
import com.nami.store.api.service.RemoteUserService;
import com.nami.store.api.response.user.UserDTO;
import com.nami.web.dto.user.UserAddDTO;
import com.nami.web.dto.user.UserLoginDTO;
import com.nami.web.facade.UserFacadeService;
import com.nami.web.vo.user.UserInfoVO;
import com.nami.web.vo.user.UserLoginVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserFacadeServiceImpl implements UserFacadeService {

    @DubboReference
    RemoteUserService remoteUserService;

    @Override
    public UserLoginVO login(UserLoginDTO dto) {
        UserDTO exist = remoteUserService.getUserByMobile(dto.getMobile());
        if (exist == null) {
            throw new ServiceException("用户不存在！");
        }
        UserLoginVO vo = new UserLoginVO();
        vo.setUserId(exist.getUserId());

//        StpUtil.login(exist.getUserId());
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(Long.valueOf(exist.getUserId()));
        loginUser.setClientKey("fdsdfdf2323hfdgk5645");
        loginUser.setDeviceType("ios");
        loginUser.setUserType(UserType.APP_USER.getUserType());
        SaLoginModel model = new SaLoginModel();
        model.setDevice("pc");
        // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
        // 例如: 后台用户30分钟过期 app用户1天过期


        model.setExtra(LoginHelper.APP_KEY, exist.getAppKey());
        model.setExtra(LoginHelper.USER_KEY, exist.getUserId());
        model.setExtra(LoginHelper.CLIENT_KEY, "fdsdfdf2323hfdgk5645");
        // 生成token
        LoginHelper.login(loginUser, model);

        String tokenValue = StpUtil.getTokenValue();
        // 调用注册中心，获取服务列表，
        vo.setGateUrl("ws://localhost:30000/ws");
        vo.setToken(tokenValue);
        return vo;
    }


    @Override
    public boolean register(UserAddDTO addDTO) {
        UserDTO userByMobile = remoteUserService.getUserByMobile(addDTO.getMobile());
        if (userByMobile != null) {
            throw new ServiceException("用户已注册！");
        }

        UserAddRequest request = new UserAddRequest();
        request.setMobile(addDTO.getMobile());
        request.setAccount(addDTO.getMobile());
        request.setPassword(addDTO.getPassword());
        request.setGender(addDTO.getGender());
        request.setEmail(addDTO.getEmail());
        request.setAppKey(addDTO.getAppKey());
        request.setUserType(10);
        request.setStatus(10);
        request.setAvatarUrl(addDTO.getAvatar());
        request.setNickname(addDTO.getNickname());
        request.setRealName(addDTO.getNickname());

        request.setUserId(IdUtil.getSnowflakeNextId() + "");
        request.setSalt(RandomUtil.randomString(6));
        return remoteUserService.save(request);
    }

    @Override
    public UserInfoVO getUserById(String userId) {
        if (userId == null) {
            return null;
        }
        UserDTO exist = remoteUserService.getUserByUserId(userId);
        if (exist == null) {
            throw new ServiceException("用户不存在！");
        }
        UserInfoVO vo = new UserInfoVO();
        vo.setUserId(userId);
        vo.setAvatar(exist.getAvatarUrl());
        vo.setNick(exist.getNickname());
        vo.setMobile(exist.getMobile());
        return vo;
    }

}
